<template>
  <div class="page luckdraw-invite-poster">
    <div class="luckdraw-invite-poster-container">
      <img :src="imgUrl" mode="widthFix" @click="previewImg"/>
      <div class="btn-section">
        <m-button text="保存图片" @onClick="onSaveImageClick"></m-button>
      </div>
    </div>
  </div>
</template>

<script>
import MButton from 'comp/m-button'
import { downloadFile, saveImageToPhotosAlbum } from '@/utils/wx'
import mixins from 'comp/mixins'

export default {
  mixins: [mixins],
  components: {
    MButton
  },
  data () {
    return {
      imgUrl: ''
    }
  },
  mounted () {
    this.resetData()
    this.onDownloadFile()
  },
  methods: {
    /**
     * 下载图片到本地
     */
    async onDownloadFile () {
      const posterUrl = this.$root.$mp.query.posterUrl
      const ret = await downloadFile({
        url: decodeURIComponent(posterUrl),
        toastText: '海报生成中...'
      })
      if (ret.statusCode === 200) {
        this.imgUrl = ret.tempFilePath
      }
    },
    /**
     * 保存图片到相册
     */
    onSaveImageClick () {
      saveImageToPhotosAlbum({
        filePath: this.imgUrl
      })
    },
    /**
     * 预览图片
     */
    previewImg () {
      const imgArr = [this.imgUrl]
      wx.previewImage({
        current: imgArr[0], // 当前图片地址
        urls: imgArr // 所有要预览的图片的地址集合，数组形式
      })
    }
  }
}
</script>

<style lang="less" scoped>
.luckdraw-invite-poster {
  &-container {
    padding: 30rpx 30rpx 114rpx;
  }
  img {
    width: 100%;
  }
  .btn-section {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>
